package org.tio.sitexxx.web.server.controller.base;

import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.http.common.HttpRequest;
import org.tio.http.server.annotation.RequestPath;
import org.tio.sitexxx.service.init.PropInit;
import org.tio.sitexxx.service.init.RedisInit;
import org.tio.sitexxx.service.service.conf.ConfService;
import org.tio.sitexxx.service.vo.Const;
import org.tio.sitexxx.service.vo.topic.TopicVo;
import org.tio.utils.jfinal.P;
import org.tio.utils.resp.Resp;

/**
 * @author tanyaowu
 * 2016年6月29日 下午7:53:59
 */
@RequestPath(value = "/config")
public class ConfigController {
	@SuppressWarnings("unused")
	private static Logger log = LoggerFactory.getLogger(ConfigController.class);

	/**
	 * @param args
	 * @author tanyaowu
	 */
	public static void main(String[] args) {

	}

	/**
	 *
	 * @author tanyaowu
	 */
	public ConfigController() {
	}

	/**
	 * 更新app-xxx.properties文件
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/update")
	public Resp update(HttpRequest request) throws Exception {
		P.clear();
		PropInit.init();
		return Resp.ok();
	}

	/**
	 * 清空conf缓存
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/clearConf")
	public Resp clearConf(HttpRequest request) throws Exception {
		ConfService.clearCache();

		RedissonClient redisson = RedisInit.get();
		RTopic topic1 = redisson.getTopic(Const.Topic.COMMON_TOPIC);
		TopicVo topicVo = new TopicVo();
		topicVo.setType(TopicVo.Type.CLEAR_CONF);
		topic1.publish(topicVo);

		return Resp.ok();
	}

	/**
	 * 清空所有配置项：包括conf、conf_dev表、app.properties、app-env.properties、app-host.properties
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/clearAll")
	public Resp clearAll(HttpRequest request) throws Exception {
		clearConf(request);
		update(request);
		return Resp.ok();
	}
}
